package com.itheima.job;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 定时清理垃圾图片功能
 */
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg(){
        //拿到jedis操作对象
        Jedis jedis = jedisPool.getResource();
        //拿到冗余图片名称集合
        Set<String> sdiff = jedis.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff != null){
            for (String s : sdiff) {
                //删除七牛云上的垃圾图片
                QiniuUtils.deleteFileFromQiniu(s);
                //清理redis中的垃圾图片
                jedis.srem(RedisConstant.SETMEAL_PIC_RESOURCES,s);
            }
        }
        //归还jedisPool池连接
        jedis.close();
    }

}
